package com.wavercaz.freeforum.controllers;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wavercaz.freeforum.pojo.CommonSearchParam;
import com.wavercaz.freeforum.pojo.Post;
import com.wavercaz.freeforum.pojo.PostFeedback;
import com.wavercaz.freeforum.services.PostFeedbackService;
import com.wavercaz.freeforum.services.PostService;
import com.wavercaz.freeforum.utils.JWTUtil;
import com.wavercaz.freeforum.utils.QueryUtil;
import com.wavercaz.freeforum.utils.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class PostFeedbackController {
    @Autowired
    private  PostFeedbackService postFeedbackService;
    @Autowired
    private PostService postService;

    // 增
    @PutMapping("/postFeedback")
    @Transactional
    public ResponseResult save(@RequestHeader("Authorization") String token,@RequestBody PostFeedback postFeedback) {
        postFeedback.setUserId(Integer.parseInt(JWTUtil.verifyToken(token).getClaim("id").asString()));
        Post post = postService.getById(postFeedback.getPostId());
        Boolean result = postFeedbackService.save(postFeedback);
        if(!result) return new ResponseResult(HttpStatus.NO_CONTENT.value(), "操作失败");
        else return new ResponseResult(HttpStatus.OK.value());
    }

    // 删
    @DeleteMapping("/postFeedback")
    public ResponseResult delete(@RequestBody List<Integer> ids) {
        boolean result = postFeedbackService.removeByIds(ids);
        if(!result) return new ResponseResult(HttpStatus.NO_CONTENT.value(), "删除失败");
        else return new ResponseResult(HttpStatus.OK.value(), "删除成功");
    }

    // 改
    @PostMapping("/postFeedback")
    public ResponseResult update(@RequestHeader("Authorization") String token,@RequestBody PostFeedback postFeedback) {
        postFeedback.setUserId(Integer.parseInt(JWTUtil.verifyToken(token).getClaim("id").asString()));
        QueryWrapper<PostFeedback> queryWrapper = new QueryWrapper<PostFeedback>()
                .eq("post_id",postFeedback.getPostId())
                .eq("user_id",postFeedback.getUserId());
        PostFeedback data = postFeedbackService.getOne(queryWrapper);
        postFeedback.setId(data.getId());
        Boolean result = postFeedbackService.updateById(postFeedback);
        if(!result) return new ResponseResult(HttpStatus.NO_CONTENT.value(), "操作失败");
        else return new ResponseResult(HttpStatus.OK.value());
    }

    // 查
    @GetMapping("/postFeedback/{id}")
    public ResponseResult get(@PathVariable Integer id ) {
        PostFeedback postFeedback = postFeedbackService.getById(id);
        if(postFeedback == null) return new ResponseResult(HttpStatus.NO_CONTENT.value(), "无此相关信息");
        else return new ResponseResult(HttpStatus.OK.value(), postFeedback);
    }

    // 列表
    @PostMapping("/postFeedbackList")
    public ResponseResult list(@RequestBody(required = false) CommonSearchParam commonSearchParam) {
        Object data = null;
        if(commonSearchParam != null) {
            QueryWrapper queryWrapper = QueryUtil.getQueryWrapper(commonSearchParam.getSearches());
            if(commonSearchParam.getPageNum()!=null && commonSearchParam.getPageSize()!=null)
                data= postFeedbackService.page(new Page<>(commonSearchParam.getPageNum(), commonSearchParam.getPageSize()), queryWrapper);
            else data= postFeedbackService.list(queryWrapper);
        }
        else data= postFeedbackService.list();
        return new ResponseResult(HttpStatus.OK.value(), data);
    }
}
